<?php

namespace app\controllers;

use yii\web\Controller;
use yii\data\Pagination;
use app\models\Rs_raised;
use app\models\Uc_user;
use Yii;

class RaisedController extends Controller
{
    //禁用原有布局
    public $layout="main2";
    //显示
    public function actionShow($status = 'all')
    {

        RegisterController::prepare();

        $raiseds = $this->getRaised($status);
        return $this->render('raised',[
            'raiseds' => $raiseds,
            'status' => $status
            ]);
    }
    //显示聚募概况
    public function actionShowDetail($raised_id)
    {

        RegisterController::prepare();


        $raised = Rs_raised::findOne([
            'raised_id' => $raised_id,
            'del_flag' => 0
            ]);
        return $this->render('raised-detail',[
            'raised' => $raised
            ]);
    }
    //我要融资界面
    public function actionSelect()
    {
        RegisterController::prepare();
        return $this->render('select');
    }
    //项目聚募数据
    public function getRaised($status = 'all')
    {      
        //全部项目
        if($status == 'all')
        {
            $query = Rs_raised::find()
            ->where([
                'del_flag' => 0,
                'type' => [0,1,2]
                ]);
        }   
        //聚募中
        else if($status == 'progress')
        {
            $query = Rs_raised::find()
            ->where([
                'del_flag' => 0,
                'type' => 1
                ]);
        }
        //预热中
        else if($status == 'preheat')
        {
            $query = Rs_raised::find()
            ->where([
                'del_flag' => 0,
                'type' => 0
                ]);
        }
        //聚募成功
        else if($status == 'succeed')
        {
            $query = Rs_raised::find()
            ->where([
                'del_flag' => 0,
                'type' => 2
                ]);
        }
        $pagination = new Pagination([
            'defaultPageSize' => 4,
            'totalCount' => $query->count()
            ]);
        $raiseds = $query->orderBy('raised_id DESC')
        ->offset($pagination->offset)
        ->limit($pagination->limit)
        ->all();
        //匹配用户id和用户名，计算进度
        foreach ($raiseds as $key => $raised)
        {
            $query1 = Uc_user::findOne([
                'user_id' => $raised->leader_id
                ]);
            $query2 = Uc_user::findOne([
                'user_id' => $raised->referrer_id
                ]);
            if(null == $query1)
            {
               $raised->leader_name = "暂无";
            }
            else
            {
               $raised->leader_name = $query1->user_name;
            }
            if(null == $query2)
            {
               $raised->referrer_name = "暂无";
            }
            else
            {
               $raised->referrer_name = $query2->user_name;
            }
            //进度百分比，保留2位小数
            $progress = $raised->raisedMoney/$raised->totalMoney*100;
            $raised->progress = round($progress, 2);
            $raised->days = floor((strtotime($raised->expire)-time())/86400);
        }
        return [
        'raiseds' => $raiseds,
        'pagination' => $pagination,
        'total' => $query->count()
        ];
    }
}